<template>
  <x-dialog :title="title" :show.sync="visible" v-if="visible" :before-close="handleClose" width="35%">
    <el-form :model="params" :rules="rules" ref="params" class="demo-form-inline" label-position="top">
      <el-row :gutter="36">
        <el-col :span="24">
          <el-form-item :label="$t('financialSynergy.invoice.logisticsCompany')" prop="logisticalCompany">
            <x-select
              :placeholder="'请选择'"
              v-model="params.logisticalCompany"
              :options="dictOptions['srm_delivery_note.logistical_company']"
              style="width: 100%"
            ></x-select>
            <!-- <el-input
              v-model="params.logisticalCompany"
              :placeholder="$t('financialSynergy.invoice.inputPlaceholder')"
              clearable
              maxlength="50"
            ></el-input> -->
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('financialSynergy.invoice.logisticsId')" prop="logisticalNumber">
            <el-input
              v-model="params.logisticalNumber"
              :placeholder="$t('financialSynergy.invoice.inputPlaceholder')"
              clearable
              maxlength="50"
            ></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer">
      <el-button @click="handleClose"> {{ $t('common.cancel') }} </el-button>
      <el-button
        type="primary"
        @click="
          loading = true
          handleSubmit()
        "
        :loading="loading"
      >
        {{ $t('common.save') }}
      </el-button>
    </span>
  </x-dialog>
</template>
<script>
  import { throttle } from '@package/utils'
  import { EnumApiCode } from '../../../../../../globalEnum'
  import { apiPostInvoiceMail } from '../api/invoiceManagement'

  export default {
    name: 'InvocieLogistics',
    props: {
      dictOptions: {
        type: Object,
        default: () => ({})
      }
    },
    data() {
      return {
        visible: false,
        loading: false,
        title: '物流信息',
        invocieId: '',
        params: {
          logisticalCompany: '',
          logisticalNumber: ''
        },
        rules: {
          logisticalCompany: {
            required: true,
            message: this.$t('financialSynergy.invoice.selectPlaceholer'),
            trigger: ['blur', 'change']
          },
          logisticalNumber: [
            { required: true, message: this.$t('financialSynergy.invoice.inputPlaceholder'), trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      handleOpen(info) {
        this.visible = true
        if (info?.id) {
          this.invocieId = info?.id
        }
      },
      handleSubmit: throttle(function () {
        this.loading = true
        this.$refs.params.validate(async (valid) => {
          if (valid) {
            const param = {
              ...this.params,
              invoiceId: this.invocieId
            }
            try {
              const { code, msg } = await apiPostInvoiceMail(param)
              this.loading = false
              if (code === EnumApiCode.Success) {
                this.$message.success(this.$t('common.opeartionSuccess'))
                this.handleClose()
                this.$emit('handleSuccess')
              } else {
                this.$message.error(msg)
              }
            } finally {
              this.loading = false
            }
          } else {
            this.loading = false
            return false
          }
        })
      }, 1000),
      handleClose() {
        this.$refs.params.resetFields()
        this.loading = false
        this.visible = false
      }
    }
  }
</script>
